import Vue from 'vue'
import Vuex from 'vuex'
import {
  testQuestionBankQuery,
  testQuestionBankDelete,
  testQuestionBankAdd,
  testQuestionBankUpdate,
} from '@/api/admin/exam/testQuestionBank'

import {
  testSubjectsQuery,
  testSubjectsDelete,
  testSubjectsAdd,
  testSubjectsBatchDelete,
  testSubjectsUpdate,
  testSubjectsQueryById,
} from '@/api/admin/exam/testSubjects'

Vue.use(Vuex)

const state = {
  activeName: 'manage',
  activeRow: {},
}

const getters = {}

const actions = {
  //题库增删改查
  async testQuestionBankList({ commit }, listQuery) {
    let res = await testQuestionBankQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testQuestionBankDel({ commit }, row) {
    let res = await testQuestionBankDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testQuestionBankADD({ commit }, data) {
    let res = await testQuestionBankAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testQuestionBankUPDATE({ commit }, data) {
    let res = await testQuestionBankUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //试题增删改查
  async testSubjectsList({ commit }, listQuery) {
    let res = await testSubjectsQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testSubjectsById({ commit }, id) {
    let res = await testSubjectsQueryById(id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testSubjectsDel({ commit }, row) {
    let res = await testSubjectsDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testSubjectsBatchDel({ commit }, row) {
    let res = await testSubjectsBatchDelete(row);
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testSubjectsADD({ commit }, data) {
    let res = await testSubjectsAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async testSubjectsUPDATE({ commit }, data) {
    let res = await testSubjectsUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  SET_ACTIVENAME: (state, d) => {
    state.activeName = d
  },
  SET_ACTIVEROW: (state, d) => {
    state.activeRow = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
